package com.byCreatetion.adabter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;


import com.byCreation.t_globe.PhotoActivity;
import com.byCreation.t_globe.R;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;



import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//=====Gallery Adabter
	
public	class ImageGalleryAdapter extends BaseAdapter {
		
		private Context activity;
		private LayoutInflater inflater=null;
		
		private volatile boolean running = true;
		 ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();
		
		
		 
			private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
			private ImageLoader imageLoader = ImageLoader.getInstance();
			private DisplayImageOptions options;
		 
		 
		 
		
		    public ImageGalleryAdapter(Activity a,ArrayList al ) {
	    	activity=a;
			MyArrList=al;
			inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			
			options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.app_icon)
			.showImageForEmptyUri(R.drawable.app_icon)
			.showImageOnFail(R.drawable.app_icon).delayBeforeLoading(100)
			.cacheInMemory().cacheOnDisc()
			.bitmapConfig(Bitmap.Config.RGB_565)
			.displayer(new RoundedBitmapDisplayer(0)).build();
			initImageLoader(activity);

			
			
		 }
		
		 private class ViewHolder {
			
				private ImageView imageView;

			}
		
		
		@Override
		public int getCount() {
			return MyArrList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View vi = convertView;
			final ViewHolder holder;

			if (convertView == null) {
				vi = inflater.inflate(R.layout.item_gallery_image, null);
				holder = new ViewHolder();
				holder.imageView = (ImageView) vi.findViewById(R.id.image);

				vi.setTag(holder);

			} else {

				holder = (ViewHolder) vi.getTag();
				
			}

			try {
				
				imageLoader.displayImage(MyArrList.get(position).get("img").toString(), holder.imageView, options,animateFirstListener);
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			
			return vi;
		}
		
		
		

		public void initImageLoader(Context context) {
			// This configuration tuning is custom. You can tune every option, you
			// may tune some of them,
			// or you can create default configuration by
			// ImageLoaderConfiguration.createDefault(this);
			// method.
			ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
					context)
					.memoryCacheExtraOptions(800, 480)
					// default = device screen dimensions
					.threadPoolSize(3)
					// default
					.threadPriority(Thread.NORM_PRIORITY - 1)
					// default
					.tasksProcessingOrder(QueueProcessingType.FIFO)
					// default
					.denyCacheImageMultipleSizesInMemory()
					.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
					.memoryCacheSize(2 * 1024 * 1024)
					.discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
					.discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
					.imageDownloader(new BaseImageDownloader(context)) // default

					.build();
			// Initialize ImageLoader with configuration.
			ImageLoader.getInstance().init(config);
		}

		@SuppressWarnings("unused")
		private static class AnimateFirstDisplayListener extends
				SimpleImageLoadingListener {

			final static List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

			@Override
			public void onLoadingComplete(String imageUri, View view,
					Bitmap loadedImage) {
				if (loadedImage != null) {
					ImageView imageView = (ImageView) view;
					boolean firstDisplay = !displayedImages.contains(imageUri);
					//if (firstDisplay) {
						FadeInBitmapDisplayer.animate(imageView, 500);
						//displayedImages.add(imageUri);
					//}
				}
			}
		}

		
		
		
	}	

